/**
 * @Title: FuncServiceImpl
 * @Package cn.com.yiqi.wsc.service.func.impl
 * @Description: TODO
 * Copyright: Copyright (c) 2016
 * Company: 成都壹柒互动科技有限公司
 * @author 会员系统组-胥钰淋
 * @date 2017/2/15 11:39
 * @version V1.0
 */
package cn.com.yiqi.wsc.service.func.impl;

import cn.com.yiqi.wsc.dao.func.FuncDao;
import cn.com.yiqi.wsc.dao.func.FuncGroupDao;
import cn.com.yiqi.wsc.entity.func.FuncEntity;
import cn.com.yiqi.wsc.entity.func.FuncGroupEntity;
import cn.com.yiqi.wsc.service.func.FuncService;
import cn.com.yiqi.wsc.service.func.dataobject.FuncTreeNodeDO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 会员系统组-胥钰淋
 * @ClassName: FuncServiceImpl
 * @Description: TODO
 * @date 2017/2/15 11:39
 */
@Service
public class FuncServiceImpl implements FuncService {

    private static final String FUNC_PREFIX = "func_";
    private static final String FUNC_GROUP_PREFIX = "group_";
    private static final String FUNC_TYPE = "func";
    private static final String FUNC_GROUP_TYPE = "group";

    @Autowired
    private FuncDao funcDao;
    @Autowired
    private FuncGroupDao funcGroupDao;
    @Override
    public List<FuncTreeNodeDO> getFuncTreeNodeList(String pId) {

        long dbPid = 0;
        boolean isGroup = true;
        if (StringUtils.isNotEmpty(pId)) {
            if (pId.startsWith(FUNC_PREFIX)) {
                isGroup = false;
                dbPid = Long.parseLong(pId.replace(FUNC_PREFIX, ""));
            } else if (pId.startsWith(FUNC_GROUP_PREFIX)) {
                dbPid = Long.parseLong(pId.replace(FUNC_GROUP_PREFIX, ""));
            }
        }
        List list;
        if (isGroup) {
            list = funcGroupDao.selectFuncGroupList(dbPid);
        } else {
            list = funcDao.selectFuncList(dbPid);
        }
        List<FuncTreeNodeDO> resultList = new ArrayList<>();
        for (Object obj :
                list) {
            FuncTreeNodeDO treeNode = new FuncTreeNodeDO();
            treeNode.setFuncObj(obj);
            if (obj instanceof FuncEntity) {
                treeNode.setType(FUNC_TYPE);
                treeNode.setId(((FuncEntity) obj).getId());
                treeNode.setName(((FuncEntity) obj).getName());
                treeNode.setTreeNodeId(FUNC_PREFIX + ((FuncEntity) obj).getId());
            } else if (obj instanceof FuncGroupEntity) {
                treeNode.setType(FUNC_GROUP_TYPE);
                treeNode.setId(((FuncGroupEntity) obj).getId());
                treeNode.setName(((FuncGroupEntity) obj).getName());
                treeNode.setTreeNodeId(FUNC_GROUP_PREFIX + ((FuncGroupEntity) obj).getId());
            }
            resultList.add(treeNode);
        }
        return resultList;
    }

}
